# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: save_data_2_file.py
  @time: 2022/5/24 11:28
  @desc:
"""
import csv
import numpy as np


#

def save_data_2_file_numpy(file_path, x_data, y_data):
    a = np.asarray([x_data, y_data])
    np.savetxt(file_path, a.T, delimiter=",")


def save_data_2_file(file_path, header, x_data, y_data):
    with open(file_path, 'w', newline='', encoding="utf_8_sig") as csvfile:
        writer = csv.writer(csvfile)
        # 写入表格的标题
        writer.writerow(header)
        # 循环写入表格的内容
        for i in range(len(x_data)):
            writer.writerow([x_data[i], y_data[i]])


def save_data_2_file_exper(file_path, header, x_data, y_data=None, y1_data=None):
    try:
        with open(file_path, 'w', newline='', encoding="utf_8_sig") as csvfile:
            writer = csv.writer(csvfile)
            # 写入表格的标题
            writer.writerow(header)
            # 循环写入表格的内容
            for i in range(len(x_data)):
                if y_data and y1_data:
                    writer.writerow([x_data[i], y_data[i], y1_data[i]])
                else:
                    writer.writerow([x_data[i]])
    except Exception as e:
        print("eee", e)


def save_spot_data_2_file(file_path, para_name, para_value):
    with open(file_path, 'a+', newline='', encoding="utf_8_sig") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow([para_name, para_value])


def saveData2CSV(file_path, data_content, header=None):
    with open(file_path, 'w', newline='', encoding="utf_8_sig") as csvfile:
        writer = csv.writer(csvfile)
        # 写入表格的标题
        if header:
            writer.writerow(header)
        # 循环写入表格的内容
        for i in range(len(data_content)):
            row_data = []
            for j in range(len(data_content[0])):
                row_data.append(data_content[i][j])
            writer.writerow(row_data)

#
# address_path = r"C:\Users\Jingw\Desktop\demo.csv"
#
# saveData2CSV(address_path,["demo1","demo2"],[[1,2],[3,4]])
